package com.example.electricbicycle.controller;

import com.example.electricbicycle.service.CustomerService;
import com.example.electricbicycle.service.CustomerServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "DeleteCustomerServlet", value = "/deleteCustomer")
public class DeleteCustomerServlet extends HttpServlet {
    private final CustomerService customerService = new CustomerServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idParam = request.getParameter("id");
        if (idParam != null && !idParam.isEmpty()) {
            int customerId = Integer.parseInt(idParam);
            int result = customerService.deleteCustomer(customerId);
            if (result > 0) {
                request.getSession().setAttribute("successMsg", "客户删除成功！");
            } else {
                request.getSession().setAttribute("errorMsg", "删除客户失败，请重试！");
            }
        }
        response.sendRedirect(request.getContextPath() + "/CustomerListServlet");
    }
}